﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ww.form.wwf;
using ww.lis.lisbll.sam;
using ww.wwf.wwfbll;
using System.Collections;
namespace ww.form.lis.sam.sq
{
    public partial class SamplingBarCodeForm : SysBaseForm
    {
        ApplyBLL bllApply = new ApplyBLL();//申请规则
        DeptBll deptbll = new DeptBll();
        DataTable dtMain = new DataTable();
        DataTable dtResult = new DataTable();
        private ww.wwf.wwfbll.UserdefinedFieldUpdateForm gridMain = new ww.wwf.wwfbll.UserdefinedFieldUpdateForm("6");
        private ww.wwf.wwfbll.UserdefinedFieldUpdateForm gridResult = new ww.wwf.wwfbll.UserdefinedFieldUpdateForm("7");
        string str申请单id = "";

        public SamplingBarCodeForm()
        {
            InitializeComponent();
            this.dataGridViewItem.AutoGenerateColumns = false;
        }

        private void SamplingBarCodeForm_Load(object sender, EventArgs e)
        {
            try
            {
                if (bllApply.str_WWF_SYS("PrintBarCode") == "1") ck_AutoPrint.Checked = true;

                this.cmb_dept.DataSource = deptbll.BllDeptDTuse(); //申请部门
                this.cmb_dept.DisplayMember = "fname";
                this.cmb_dept.ValueMember = "fdept_id";
                this.cmb_dept.SelectedIndex = -1;

                bt_find_Click(null, null);
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }

        #region 方法
        private void GetMailList()
        {
            this.bindingSource1.DataSource = this.bllApply.BllSamplingMainDT("t2.fsample_barcode='" + this.tbfsample_barcode.Text + "'");
            this.dataGridViewApply.DataSource = this.bindingSource1;
            this.tbfsample_barcode.Text = "";
            this.ActiveControl = this.tbfsample_barcode;
        }
        private void GetResultList(string fapply_id)
        {
            this.dtResult = this.bllApply.BllSamplingResultDT(fapply_id);
            this.dataGridViewItem.DataSource = this.dtResult;
        }
        #endregion

        private void toolStripMenuItem_applygs_Click(object sender, EventArgs e)
        {
            try
            {
                gridMain.ShowDialog();
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }

        private void toolStripMenuItem_checkidtemgs_Click(object sender, EventArgs e)
        {
            try
            {
                gridResult.ShowDialog();
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }

        private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            try
            {
                HelpForm help = new HelpForm("68f5df6b5aa14f86829ab0995b58dc59");
                help.Show();
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }

        private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bindingSource1_PositionChanged(object sender, EventArgs e)
        {
            try
            {
                DataRowView rowCurrent = (DataRowView)this.bindingSource1.Current;
                if (this.dtResult.Rows.Count > 0)
                    this.dtResult.Clear();
                if (rowCurrent != null)
                {
                    string strfapply_id = rowCurrent["fapply_id"].ToString();
                    str申请单id = strfapply_id;
                    GetResultList(strfapply_id);
                }

            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }

        private void bt_find_Click(object sender, EventArgs e)
        {
            try
            {
                string strDateif = " (t1.fapply_time>='" + fjy_dateDateTimePicker1.Value.ToString("yyyy-MM-dd") + " 00:00:00" + "' and t1.fapply_time<='" + fjy_dateDateTimePicker2.Value.ToString("yyyy-MM-dd") + " 24:00:00" + "')";
                string fhz_dept = "";//病人部门
                if (object.Equals(cmb_dept.SelectedValue, null))//病人部门
                    fhz_dept = "";
                else
                    fhz_dept = cmb_dept.SelectedValue.ToString();
                if (fhz_dept == "" || fhz_dept == null)
                    fhz_dept = "";
                else
                    fhz_dept = " and (t1.fapply_dept_id='" + fhz_dept + "')";

                string fhz_zyh = tbfhz_zyh.Text.Trim();
                if (fhz_zyh == "" || fhz_zyh == null)
                    fhz_zyh = "";
                else
                    fhz_zyh = " and (t1.fhz_zyh='" + fhz_zyh + "')";

                string fprint_flag = "";
                if (cmb_dystate.Text == "全部") fprint_flag = "";
                if (cmb_dystate.Text == "已打印") fprint_flag = " and (t2.fprint_flag=1)";
                if (cmb_dystate.Text == "未打印") fprint_flag = " and (isnull(t2.fprint_flag,0)=0)";

                dtMain = this.bllApply.BllSamplingMainDT(strDateif + fhz_dept + fhz_zyh + fprint_flag);
                this.bindingSource1.DataSource = dtMain;
                dataGridViewApply.DataSource = bindingSource1;

                for (int intGrid = 0; intGrid < this.dataGridViewApply.Rows.Count; intGrid++)
                {
                    string fstate = this.dataGridViewApply.Rows[intGrid].Cells["fstate"].Value.ToString().Trim();
                    if (fstate == "1")
                        this.dataGridViewApply.Rows[intGrid].DefaultCellStyle.ForeColor = Color.Black; //System.Drawing.
                    else if (fstate == "2")
                        this.dataGridViewApply.Rows[intGrid].DefaultCellStyle.ForeColor = Color.Blue; //System.Drawing.
                    else if (fstate == "3")
                        this.dataGridViewApply.Rows[intGrid].DefaultCellStyle.ForeColor = Color.Red; //System.Drawing.
                    else
                        this.dataGridViewApply.Rows[intGrid].DefaultCellStyle.ForeColor = Color.Black; //System.Drawing.
                }
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            try
            {
                HelpForm help = new HelpForm("68f5df6b5aa14f86829ab0995b58dc59");
                help.Show();
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                ww.form.lis.sam.sq.ApplyPrint pr = new ww.form.lis.sam.sq.ApplyPrint();
                pr.BllPrintApplyBCode(0, this.str申请单id);
                //记录打印记录
                ww.wwf.wwfbll.WWFInit.wwfRemotingDao.DbExecuteNonQueryBySqlString(ww.wwf.wwfbll.WWFInit.strDBConn, "update SAM_SAMPLE set fprint_flag=1,fstate=1,fprint_time=convert(varchar(50),getdate(),121),fprint_count=isnull(fprint_count,0)+1 where fapply_id='" + this.str申请单id + "'");

            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                ww.form.lis.sam.sq.ApplyPrint pr = new ww.form.lis.sam.sq.ApplyPrint();
                pr.BllPrintApplyBCode(1, this.str申请单id);
            }
            catch (Exception ex)
            {
                WWMessage.MessageShowError(ex.ToString());
            }
        }

        private void tbfsample_barcode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)//或(e.KeyCode==Keys.Enter) 
            {
                try
                {
                    Cursor = Cursors.WaitCursor;
                    GetMailList();
                    if (ck_AutoPrint.Checked) toolStripButton2_Click(null, null);
                }
                catch (Exception ex)
                {
                    WWMessage.MessageShowError(ex.ToString());
                }
                finally
                {
                    Cursor = Cursors.Arrow;
                }
            }
        }

        private void dataGridViewApply_SelectionChanged(object sender, EventArgs e)
        {
            if (object.Equals(dataGridViewApply.CurrentRow, null)) return;
            str申请单id = dataGridViewApply.CurrentRow.Cells["fapply_id"].Value.ToString().Trim();
        }      
       
    }
}